/**
 * Function.java 
 * @author Matan Ginzburg 203827753
 *
 */

public class Function {

	private final int N;
	private Complex[] vals;
	private Complex[] coefs;
	private int[] alphas;

	/**
	 * alphas.length = coefs.length
	 */
	public Function(int N, Complex[] coefs, int[] alphas) {
		
		this.N = N;
		
		this.coefs = new Complex[coefs.length];
		for(int i = 0; i<this.coefs.length; i++)
			this.coefs[i] = new Complex(coefs[i]);
		
		this.alphas = new int[alphas.length];
		for(int i = 0; i<this.alphas.length; i++)
			this.alphas[i] = alphas[i];
		
		this.vals = new Complex[N];
	}
	public Function(Complex[] vals) {
		
			this.N = vals.length;
			
			vals = new Complex[N];
			for(int i=0; i<N; i++)
				this.vals[i] = new Complex(vals[i]);
	}
	public Function(int N) {
		
		this.N = N;
		this.vals = new Complex[N];
	}
	
	public Complex apply(int i){
		int j = ((i % N) + N) % N;
		if(vals[j]!=null)
			return vals[j];
		
		if(coefs==null) {
			vals[j] = GUI.getValue(j);
			return vals[j];
		}
		
		vals[j] = new Complex();
		for(int k = 0; k<coefs.length; k++) {
			vals[j].Add(Complex.Mult(coefs[k], Complex.Chi(alphas[k], i, N)));
		}
		return vals[j];
	}
	
	public int getN() {return N;}
}
